<?php
/**
 * 文件管理
 */
namespace App\http\Models;
use Illuminate\Database\Eloquent\Model;
use App\Http\Util\UploadFile;

class File extends Model
{
    /**
     * 文件上传
     * @key 上传字段名
     * @path 上传主路径('/'结尾)
     * @fileType 附件上传类型
     * $save_path 主路径下的文件夹('/'结尾)
     * $extent_path 扩展文件夹('/'结尾)
     */
    public static function file_upload($key, $path, $fileType,$save_path = '', $extent_path = '')
    {
        $upload = new UploadFile(); // 实例化上传类
        $upload->allowExts = $fileType; //设置附件上传类型
        $upload->maxSize = 1024 * 1000 * 1000 * 20; // 设置附件上传大小20M
        $upload->savePath = $path . $save_path . $extent_path; // 设置附件上传目录
        $upload->uploadReplace = true; //存在同名是否覆盖
        $upload->saveRule = str_pad(rand(000000,999999),'6','0',STR_PAD_LEFT ).date('YmdHis',time()); //文件重命名

        if ($upload->uploadOne($key)) {
            $rs = $upload->getUploadFileInfo();
            $result = array(
                'upload' => true,
                'filename'=> $upload->saveRule.'.'.substr($key['name'], strrpos($key['name'], '.')+1),
                'info' => $rs
            );
        } else {
            $result = array(
                'upload' => false,
                'info' => $upload->getErrorMsg()
            );
        }
        return $result;
    }
}
